import {reqLogin} from '@/api'

const actions = {
  async login({commit},user){
    const res = await reqLogin(user)
    if(res.code == 200){
      commit("LOGIN",res.data)
      // 将数据存到本地缓存
      localStorage.setItem('menu',JSON.stringify(res.data.menu))
      return 'ok'
    }else{
      return Promise.reject(res.data)
    }
  }
}

const mutations = {
  LOGIN(state,data){
    state.userInfo = data
  }
}

const state = {
  userInfo:{}
}
const getters = {
  token(state){
    return state.userInfo.token ||''
  },
  menu(state){
    return state.userInfo.menu ||[]
  }
}

export default {
  namespaced:true,
  state,
  actions,
  mutations,
  getters
}